package com.whz.practise.ui.customView

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import com.whz.practise.R
import java.util.jar.Attributes

/**
 *@auther whz
 *@date 2021/4/7 21:21
 * 自定义loading加载
 * 在一个frameLayout中添加三个布局做显示隐藏处理
 *
 * 有bug不知道如何哪里出了问题，能力有限 放弃。。。。
 */
class MyLoadingView constructor(context: Context, attributeSet: AttributeSet?=null, defStyleAttr: Int=0): FrameLayout(context,attributeSet,defStyleAttr){

    //加载时显示的文字
    protected var mLoadingTextTv:TextView?=null
    var mContext:Context?=null
//    加载错误视图
    protected var mLoadError:LinearLayout?=null
//    加载错误点击时间处理
    private var mLoadingHandler:LoadingHandler?=null
//    加载view
    private var loadingView:View?=null
//    加载失败view
    private var loadingErrorView:View?=null
//    数据为空
    private var emptyView:View?=null


    fun MyLoadingView(mContext:Context,attributeSet: AttributeSet?){
        this.MyLoadingView(mContext,attributeSet,0)
    }

    fun MyLoadingView(mContext: Context,attributeSet: AttributeSet?,defStyleAttr: Int){
        this.mContext=mContext
    }

    fun setLoadingHandler(loadingHandler:LoadingHandler){
        mLoadingHandler=loadingHandler
    }

    fun setLoadingErrorView(loadingErrorView:View){
        this.removeViewAt(1)
        this.loadingErrorView=loadingErrorView
        this.loadingErrorView!!.setOnClickListener {
            if (mLoadingHandler!=null){
                mLoadingHandler!!.doRequestData()
                load()
            }
        }
        this.addView(loadingErrorView,1)
    }

    fun setLoadingView(loadingView:View){
        this.removeViewAt(0)
        this.loadingView=loadingView
        this.addView(loadingView,0)
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        mContext=context
        loadingView= inflate(mContext, R.layout.my_loading_view,null)
        loadingErrorView= inflate(mContext,R.layout.network_layout,null)
        emptyView= inflate(mContext,R.layout.empty_layout,null)
        this.addView(loadingView)
        this.addView(loadingErrorView)
        loadingErrorView!!.visibility= GONE
        emptyView!!.visibility= GONE
        initView(this)
    }

    fun setMessage(message:String){
        mLoadingTextTv!!.text=message
    }

    private fun initView(rootView:View){
        mLoadingTextTv=rootView.findViewById<TextView>(R.id.loading_text_tv)
        mLoadError=rootView.findViewById(R.id.load_error_l1)
        mLoadError!!.setOnClickListener {
            if (mLoadingHandler!=null){
                load()
                mLoadingHandler!!.doRequestData()
            }
        }
    }

    fun load(){
        loadingView!!.visibility= VISIBLE
        loadingErrorView!!.visibility= GONE
        emptyView!!.visibility= GONE
    }

    fun load(message:String){
        mLoadingTextTv!!.text=message
        loadingView!!.visibility= VISIBLE
        loadingErrorView!!.visibility= GONE
        emptyView!!.visibility= GONE
    }

    fun loadSuccess(){
        this.loadSuccess(false)
    }

    fun loadSuccess(isEmpty: Boolean){
        loadingView!!.visibility= GONE
        loadingErrorView!!.visibility= GONE
        if (isEmpty){
            emptyView!!.visibility= VISIBLE
        }else{
            emptyView!!.visibility= GONE
        }
    }

    fun loadError(){
        loadingView!!.visibility= GONE
        loadingErrorView!!.visibility= VISIBLE
    }

    interface LoadingHandler{
        fun doRequestData()
    }
}